package easy._141_LinkedListCycle;

import java.util.HashSet;
import java.util.Set;

public class LinkedListCycle {
    //不标准解法,解法集合中大多数都是不标准解法……
    public boolean hasCycle(ListNode head) {
        if(head==null) return false;
        head.val = Integer.MIN_VALUE;
        head = head.next;
        while(head!=null){
            if(head.val==Integer.MIN_VALUE){
                return true;
            }
            head.val = Integer.MIN_VALUE;
            head = head.next;
        }
        return false;
    }
    //参考方法，根据哈希表根据引用位置和内存地址进行判断
    public boolean hasCycle1(ListNode head) {
        Set<ListNode> nodes = new HashSet<>();
        while(head!=null){
            if(nodes.contains(head)){
                return true;
            }else{
                nodes.add(head);
            }
            head = head.next;
        }
        return false;
    }
}
